/**
 * Author       :       Kang, JulKi
 * E-mail       :       jkkang65@@hanmail.net
 * 
 * Ver		:	1.15
 * 
 * Last updated 2011. 9. 1
 * Copyleft () 2011, All rights is not reserved..
 */

import  java.awt.*;
import  java.awt.event.*;
import	java.awt.Color;
import  javax.swing.*;

import	java.net.*;
import	java.io.*;

import	java.lang.*;

class MyFrame extends JFrame implements ActionListener, WindowListener
{
	ServerSocket	ss = null;
	Socket		sc;
	InputStream	is;
	OutputStream	os;
	int		iPortNum = 20111;
		
	JButton         yes_bt = new JButton("Ok");
        JButton         can_bt = new JButton("Cancel");
        
        JButton[]	jGridButton; 

        JPanel          pGrid = new JPanel();
        JPanel          pMenu = new JPanel();

        BorderLayout    bl = new BorderLayout();

        Dimension	gridD;
        Dimension       screen;
        Dimension       f_size;
        int		xPos, yPos;
        int		nSteps;
        
        byte[]		pBuf;
        
        Container cp;

        public MyFrame ()
        {
                //d = new Dimension(x, y);
                screen = Toolkit.getDefaultToolkit().getScreenSize();

                socketInit();	// Socket startup                
           //     drawFrame();    // screen init
           //     eventAdd();        // Event add or Thread add

                //setResizable(false);
                //this.setVisible(true);

                //pack();
        }

        public void drawFrame()
        {
                this.setSize(800, 800);
                this.setTitle("Hello, My JFrame : " + (int)gridD.getWidth() + "x" + (int)gridD.getHeight() + "  Step(" + getNSteps() + ")");

                cp = getContentPane();

                //cp.setBackground(new Color(0, 255, 0));
                cp.setBackground(new Color(Color.white.getRGB()));
                //cp.setLayout(new GridLayout((int)d.getWidth(), (int)d.getHeight()));
                //setLayout(new GridLayout((int)d.getWidth(), (int)d.getHeight()));
                cp.setLayout(bl);
                  
                //this.add("Center", pGrid);
                //this.add(pGrid, BorderLayout.CENTER);
               // this.add(pMenu, BorderLayout.SOUTH);
                
                cp.add(pGrid, BorderLayout.CENTER);
                cp.add(pMenu, BorderLayout.SOUTH);
        
                //pGrid.setLayout(gl_1);
                pGrid.setLayout(new GridLayout((int)gridD.getWidth(), (int)gridD.getHeight()));
        
                pMenu.setLayout(new GridLayout(1, 2));
                pMenu.add(yes_bt);
                pMenu.add(can_bt);

        	jGridButton = new JButton[(int)gridD.getWidth() * (int)gridD.getHeight()];
                
                for (int i=0; i<(int)gridD.getWidth() * (int)gridD.getHeight(); i++) {
                	//System.out.println("i. = " + Integer.toString(i));

                	jGridButton[i] = new JButton();
                	//cp.add(jb);
                	//pGrid.add(jb);
                	pGrid.add(jGridButton[i]);
        
//        		for (int i=0; i<str.length(); i++) {
//        			num = bs[i] - 48;
//  	  			System.out.println("bs[" + i + "] = " + num);
//        		}	

                	jGridButton[i].setBackground(new Color(255, 0, 0));
                }
             
        	f_size = this.getSize();

                this.xPos = (int)(screen.getWidth() / 2 - f_size.getWidth() / 2);
                this.yPos = (int)(screen.getHeight() / 2 - f_size.getHeight() / 2);
                
                setLocation(this.xPos, this.yPos);
		setVisible(true);
        }

        public void changeButtonColor(int step)
        {
                this.setTitle("Hello, My JFrame : " + (int)gridD.getWidth() + "x" + (int)gridD.getHeight() + "  Step(" + step + "/" + getNSteps() + ")");
                
                for (int i=0; i<(int)gridD.getWidth() * (int)gridD.getHeight(); i++) {
                	if (pBuf[i]-48 == 0)	jGridButton[i].setBackground(new Color(Color.lightGray.getRGB()));
                	else			jGridButton[i].setBackground(new Color(Color.darkGray.getRGB()));
                }
        }
        public void eventAdd()
        {
                this.addWindowListener(this);
                yes_bt.addActionListener(this);
                can_bt.addActionListener(this);
        }

        public void socketInit()
        {
        	byte[]		bNStep_size = new byte[10];
        	byte[]		bGridX_size = new byte[10];
        	byte[]		bGridY_size = new byte[10];
        	int		iGridX_size, iGridY_size;
        	//byte[]		bRecvBuf = new byte[30];

       		try {
       			ss = new ServerSocket(iPortNum);
        		
       			System.out.println("Server with Port(" + iPortNum + ") is ready...");
       			System.out.println("Connection waiting...");

			sc = ss.accept();
			System.out.println(sc.getInetAddress() + " connected...");
			
			is = sc.getInputStream();

			//read bGridX, bGridY in the input stream
			is.read(bNStep_size, 0, 10);
			is.read(bGridX_size, 0, 10);
			is.read(bGridY_size, 0, 10);
			
			String	sNStep_size = new String(bNStep_size);
			String	sGridX_size = new String(bGridX_size);
      			String	sGridY_size = new String(bGridY_size);
        		
      			System.out.println("sNStep_size = " + sNStep_size);
      			System.out.println("sGridX_size = " + sGridX_size);
      			System.out.println("sGridY_size = " + sGridY_size);

      			sNStep_size = sNStep_size.replaceAll(" ", "");
			sGridX_size = sGridX_size.replaceAll(" ", "");
      			sGridY_size = sGridY_size.replaceAll(" ", "");
       			//System.out.println("gx = " + gx.replaceAll(" ", ""));
      			
      			System.out.println("sNStep_size = " + sNStep_size);
       			System.out.println("sGridX_size = " + sGridX_size);
       			System.out.println("sGridY_size = " + sGridY_size);
        		
       			nSteps	    = Integer.parseInt(sNStep_size);
       			iGridX_size = Integer.parseInt(sGridX_size);
       			iGridY_size = Integer.parseInt(sGridY_size);

       			System.out.println("nSteps = " + nSteps);
       			System.out.println("iGridX_size = " + iGridX_size);
       			System.out.println("iGridY_size = " + iGridY_size);
        		
       			gridD = new Dimension(iGridX_size, iGridY_size);
        		
       			System.out.println("gridD.Width = " + (int)gridD.getWidth());
       			System.out.println("gridD.Height = " + (int)gridD.getHeight());
       			
       			drawFrame();

       			//while (true) {
			for (int i=0; i<gridD.getWidth() * gridD.getHeight(); i++) {
				System.out.println("i = " + i);
       				//sc = ss.accept();
       				//System.out.println(sc.getInetAddress() + " connected...");
        		
       				BufferedReader br = new BufferedReader(new InputStreamReader(is));
       				String str = br.readLine();

       				//System.out.println("nr = " + nr);
       				//System.out.println("bRecvBuf = " + str);
       				System.out.println("str(br.readLine()) = " + str);
       				System.out.println("str.length() = " + str.length());
        		
       				//int	str_size = str.length();
        	
       				//byte[] bs = str.getBytes();
       				pBuf = str.getBytes();
/*
        		for (int i=0; i<str.length(); i++) {
        			num = bs[i] - 48;
        			System.out.println("bs[" + i + "] = " + num);
        		}
*/        		
        		//System.out.println("bRecvBuf = " + bRecvBuf.toString());
        		//System.out.println("bRecvBuf = " + bRecvBuf);
        		
        		
        			//is.close();
        			//os.close();
        			//sc.close();
       		
       				changeButtonColor(i);
       				System.out.println("Grid Button is changed...");
       			}
			
			is.close();
			sc.close();
			ss.close();
       		}
       		catch  (IOException e) {
       			e.printStackTrace();
       		}
        }
        
        public int getNSteps()
        {
        	return	nSteps;
        }
        
        public void actionPerformed(ActionEvent e)
        {
                if (e.getSource() == yes_bt)
                {
                        System.out.println("Ok button pressed...");
                }
                else if (e.getSource() == can_bt)
                {
                        System.out.println("Exit button pressed...");
                }
        }

        public void windowClosing(WindowEvent e)
        {
                System.out.println("System control button pressed to exit...");
                System.exit(0);
        }
        
        public void windowClosed(WindowEvent e) {}
        public void windowOpened(WindowEvent e) {}
        public void windowActivated(WindowEvent e) {}
        public void windowDeactivated(WindowEvent e) {}
        public void windowIconified(WindowEvent e) {}
        public void windowDeiconified(WindowEvent e) {}
}

/**
 * 
 * @@author jkkang
 *
 * start of Main()
 */
public class MyFrameMain
{
        public static void main(String [] args)
        {
                MyFrame myFrame = new MyFrame();
        }
}
